/**
 * Created by Administrator on 2017/10/31.
 */
var mongoose = require('mongoose');
var multer  = require('multer');
var path  = require('path');
const DataModel = require('../models/upload.model');

var storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, './uploads')
    },
    filename: function (req, file, cb) {
        //返回一个文件名，该文件通过表单文件上传的名称（前台设置的avatar） + 时间戳 + 原始文件的扩展名
        cb(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname));
    }
});
exports.upload = function(req,res,next){
    var cateId = req.params.cateId;
    //实例化multer
    var upload = multer({
        storage:storage
    }).single('avatar');
    upload(req,res,function(err){
        req.file.cateId = cateId
        const dataModel = new DataModel(req.file);
        dataModel.save()
            .then(data=>{
                res.json(data);
            })
        res.end('File is uoloaded');
    })
}

exports.create = function (req, res, next) {
    const dataModel = new DataModel(req.body);
    dataModel.save()
        .then(data => {
            res.json(data);
        })
}

//查，获取
exports.get = function(req,res,next){
    var id = req.params.id;

    DataModel.findById(id,function(err,data){
        res.json(data);
    })
}

//改
exports.update = function(req,res,next){
    const id = req.params.id;

    DataModel.findByIdAndUpdate(id, {$set: req.body},{new:false})
        .then(data=>{
            res.json(data);
        })
}

//获取列表记录
exports.classifyLists = function (req, res, next) {
    var ids = req.body.cateId.split(",");
    if (ids.length > 0) {
        DataModel.find({cateId: {$in: ids}}).then(data=> {
            res.json(data);
        })
    } else {
        res.json({"msg": "请求失败", "status": 404})
    }
};
//删
exports.remove = function(req,res,next){
    const id = req.params.id;
    DataModel.findByIdAndRemove(id,function(err,data){
        res.json(data);
    })
};

//列表
exports.list=function(req,res,next){
    var page=(req.body.page)?req.body.page:1;
    var limit=(req.body.limit)?req.body.limit:10;
    //定义查询条件
    var queryCondition={ };
    if(req.body.originalname&&req.body.originalname.trim().length>0){
        originalname=req.body.originalname;
        queryCondition={
            'originalname':new RegExp(originalname,'i')
        }
    }
    if(req.body.cateId && req.body.cateId.trim().length>0){
        cateId = req.body.cateId;
        // Object.assign对象合并
        queryCondition = Object.assign(queryCondition,
            {
                cateId: cateId
            }
        )
    }

    DataModel.paginate(queryCondition,{sort:{_id:-1}, page:parseInt(page),limit:parseInt(limit)},
        function(err,result){
            result.rows=result.docs;
            delete result.docs;
            res.json(result)
        });
}

exports.deletes = function(req,res,next){
    var ids = req.body.ids;
    if(ids.length>0){
        DataModel.remove({_id:{$in:ids.split(',')}})
            .then(data=>{
                res.json({"msg":"deletes success","status":200});
            })
    }else{
        res.json({"msg":"deletes fail","status":404});
    }
};